﻿using System.Collections.Generic;
using System.IO;
using Amib.Threading;
using MediaSage.Lib;
using MediaSage.Lib.MediaUpdate;
using MediaSage.Lib.Plugin;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using MediaSage.Lib.Configuration;

namespace MediaSageLibTest
{
    
    
    /// <summary>
    ///This is a test class for MediaUpdateTest and is intended
    ///to contain all MediaUpdateTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MediaUpdateTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        private static Config config;

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            config = new Config();

            // Install the plugins we are going to use
            var installer = new Installer(config);
            string InstallFile = Path.Combine(Directory.GetCurrentDirectory(), "TVDBProvider.zip");
            bool result = installer.InstallPlugin(InstallFile, false);
            Assert.AreEqual(true, result, "Installation of the TVDBProvider failed");
            InstallFile = Path.Combine(Directory.GetCurrentDirectory(), "TMDBProvider.zip");
            result = installer.InstallPlugin(InstallFile, false);
            Assert.AreEqual(true, result, "Installation of the TMDBProvider failed");
        }
        
        
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for UpdateMedia
        ///</summary>
        [TestMethod()]
        public void UpdateMediaTest()
        {
            MediaUpdate target = new MediaUpdate(config);

            // Need to create a mediaclass object and then try to update it.
            // For the item, we will set the Filename (Bogus), IMDBID and TMDB ID and let everything else get populated.
            MediaClass mc = new MediaClass(config);
            mc.Filename = "TestingFilename";
            // Movie Lincoln
            mc.IMDBID = "tt0443272";
            mc.TvdbID = 72976;
            mc.videoEntity.SaveOrUpdate();

            // TODO: Need to install plugins, minimum of TVDB and TMDB to see if we get the proper update
            // as they are metadata providers

            target.UpdateMedia("TestingFilename");
            var threadResults=config.ThreadManager.GetThreadResults();
            var runningThreads = new List<Guid>();
            foreach(KeyValuePair<Guid,IWorkItemResult<object>> kvp in threadResults)
            {
                runningThreads.Add(kvp.Key);
            }
            config.ThreadManager.WaitForResults(runningThreads);
            Assert.Inconclusive("TODO: Implement code to verify target");
        }
        
    }
}
